import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import 'ranking_item.dart';

class ATHRankingContent extends StatefulWidget {
  @override
  _ATHRankingContentState createState() => _ATHRankingContentState();
}

class _ATHRankingContentState extends State<ATHRankingContent>
    with AutomaticKeepAliveClientMixin {
  EasyRefreshController _easyRefreshController;

  // 条目总数
  int _count = 2;

  @override
  void initState() {
    super.initState();
    _easyRefreshController = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return EasyRefresh.custom(
      header: MaterialHeader(),
      footer: MaterialFooter(),
      enableControlFinishRefresh: true,
      enableControlFinishLoad: true,
      controller: _easyRefreshController,
      onRefresh: () async {
        await Future.delayed(Duration(seconds: 2), () {
          _easyRefreshController.finishRefresh(success: true);
        });
      },
      onLoad: () async {
        await Future.delayed(Duration(seconds: 2), () {
          setState(() {
            _count += 2;
          });
          _easyRefreshController.finishLoad(noMore: _count >= 10);
        });
      },
      slivers: [_buildRankingListView()],
    );
  }

  Widget _buildRankingListView() {
    return SliverList(
      delegate: SliverChildBuilderDelegate((context, index) {
        return ATHRankingItem();
      }, childCount: _count),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
